package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsHomeRecommendProductApi;
import com.itheima.sc.mapper.sms.SmsHomeRecommendProductMapper;
import com.itheima.sc.pojo.sms.HomeRecommendProduct;
import com.itheima.sc.pojo.sms.HomeRecommendProduct;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendProductImpl implements SmsHomeRecommendProductApi {

    @Autowired
    private SmsHomeRecommendProductMapper recommendProductMapper;

    //添加首页推荐
    public void addRecommendProducts(List<HomeRecommendProduct> recommendProducts) {
        for (HomeRecommendProduct recommendProduct : recommendProducts) {
            recommendProductMapper.insert(recommendProduct);
        }
    }

    //批量删除首页推荐
    public void deleteRecommendProducts(List<Long> ids) {
        recommendProductMapper.deleteBatchIds(ids);
    }
    //分页查询首页推荐
    public PageResult selectRecommendProductList(Integer page, Integer pageSize, Integer recommendStatus, String productName) {
        //构建分页查询条件
        Page<HomeRecommendProduct> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<HomeRecommendProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, HomeRecommendProduct::getRecommendStatus, recommendStatus);
        wrapper.like(StrUtil.isNotBlank(productName), HomeRecommendProduct::getProductName, productName);
        //排序
        wrapper.orderByDesc(HomeRecommendProduct::getSort);
        //查询请求
        ipage = recommendProductMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //批量修改首页推荐的状态
    public void updateRecommendProductStatus(List<Long> ids, Integer status) {
        for (Long id : ids) {
            HomeRecommendProduct RecommendProduct = new HomeRecommendProduct();
            RecommendProduct.setId(id);
            RecommendProduct.setRecommendStatus(status);
            recommendProductMapper.updateById(RecommendProduct);
        }
    }

    //修改首页推荐
    public void updateRecommendProductSort(HomeRecommendProduct RecommendProduct) {
        recommendProductMapper.updateById(RecommendProduct);
    }
}
